﻿using System;
using System.Windows.Forms;

namespace SimpleMediaManager
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            checkBoxScale.Checked = Program.Settings.VideoScale;
            videoPlayback1.OpenFile(@"F:\Incomplete\Sanford and Son\Season 01\Sanford and Son - S01E02 - Happy Birthday, Pop.avi");

            this.Size = new System.Drawing.Size(videoPlayback1.Resolution.Value.Width + 16, videoPlayback1.Resolution.Value.Height + 160);

            toolStripStatusLabelPosition.Text = getPlaybackPositionString(videoPlayback1.PlaybackPosition.Value, videoPlayback1.Length.Value);

            trackBarPosition.Maximum = (int)videoPlayback1.Length.Value.TotalSeconds;
            videoPlayback1.Play();
            buttonPause.Image = Properties.Resources.pause;
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BulkAdd form = new BulkAdd();
            form.ShowDialog();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About form = new About();
            form.ShowDialog();
        }

        private void buttonPause_Click(object sender, EventArgs e)
        {
            videoPlayback1.TogglePause();

            if (videoPlayback1.PlaybackStatus == SimpleMediaManager.Controls.PlayState.Paused)
            {
                buttonPause.Image = Properties.Resources.play;
            }
            else
            {
                buttonPause.Image = Properties.Resources.pause;
            }
        }

        private void checkBoxScale_CheckedChanged(object sender, EventArgs e)
        {
            Program.Settings.VideoScale = checkBoxScale.Checked;
            Program.Settings.Serialize(Program.SETTINGS_FILE);

            videoPlayback1.ScaleVideoFit();
        }

        //private void trackBarPosition_Scroll(object sender, EventArgs e)
        //{
        //    videoPlayback1.SetPlaybackPosition((double)trackBarPosition.Value);
        //    toolStripStatusLabelPosition.Text = getPlaybackPositionString(videoPlayback1.PlaybackPosition.Value, videoPlayback1.Length.Value);
        //}

        private void buttonPlaybackSlower_Click(object sender, EventArgs e)
        {
            if (videoPlayback1.PlaybackRate > 0.25)
            {
                videoPlayback1.PlaybackRate = videoPlayback1.PlaybackRate - 0.25;
                videoPlayback1.SetPlaybackRate(videoPlayback1.PlaybackRate);
            }
            string rate = videoPlayback1.PlaybackRate.ToString();
            if (rate.Length == 1) rate += ".0";
            toolStripStatusLabelPlaybackSpeed.Text = rate + "x";
        }

        private void buttonPlaybackFaster_Click(object sender, EventArgs e)
        {
            if (videoPlayback1.PlaybackRate < 5.0)
            {
                videoPlayback1.PlaybackRate = videoPlayback1.PlaybackRate + 0.25;
                videoPlayback1.SetPlaybackRate(videoPlayback1.PlaybackRate);
            }
            string rate = videoPlayback1.PlaybackRate.ToString();
            if (rate.Length == 1) rate += ".0";
            toolStripStatusLabelPlaybackSpeed.Text = rate + "x";
        }

        private string getPlaybackPositionString(double current, TimeSpan total)
        {
            string timeTotal = "";
            string timeCurrent = "";
            int timeCurrentHours = (int)((current / 60) / 60);
            int timeCurrentMinutes = (int)((current / 60) % 60);
            int timeCurrentSeconds = (int)(current % 60);
            if (total.Hours > 0)
            {
                timeCurrent = timeCurrentHours.ToString().PadLeft(2, '0') + ":";
                timeTotal = total.Hours.ToString().PadLeft(2, '0') + ":";
            }
            timeCurrent += timeCurrentMinutes.ToString().PadLeft(2, '0') + ":";
            timeTotal += total.Minutes.ToString().PadLeft(2, '0') + ":";
            timeCurrent += timeCurrentSeconds.ToString().PadLeft(2, '0');
            timeTotal += total.Seconds.ToString().PadLeft(2, '0');

            return timeCurrent + "/" + timeTotal;
        }

        private void trackBarPosition_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Y < 20)
            {
                videoPlayback1.Pause();

                int x = e.X - 13;
                int value = (int)(trackBarPosition.Maximum * (double)((x) / (double)(trackBarPosition.Width - 26)));

                if (value <= trackBarPosition.Maximum)
                {
                    if (value >= trackBarPosition.Minimum)
                    {
                        trackBarPosition.Value = value;
                    }
                    else
                    {
                        trackBarPosition.Value = trackBarPosition.Minimum;
                    }
                }
                else
                {
                    trackBarPosition.Value = trackBarPosition.Maximum;
                }

                videoPlayback1.SetPlaybackPosition((double)trackBarPosition.Value);
                toolStripStatusLabelPosition.Text = getPlaybackPositionString(videoPlayback1.PlaybackPosition.Value, videoPlayback1.Length.Value);
            }
        }

        private void trackBarPosition_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Y < 20) videoPlayback1.TogglePause();
        }

        private void buttonMute_Click(object sender, EventArgs e)
        {
            videoPlayback1.ToggleMute();

            if (videoPlayback1.Mute)
            {
                buttonMute.Image = Properties.Resources.volume_mute;
                trackBarVolume.Enabled = false;
            }
            else
            {
                buttonMute.Image = Properties.Resources.volume_max;
                trackBarVolume.Enabled = true;
            }
        }

        private void buttonFullscreen_Click(object sender, EventArgs e)
        {
            videoPlayback1.ToggleFullScreen();
        }

        private void buttonStop_Click(object sender, EventArgs e)
        {
            videoPlayback1.Stop();
            buttonPause.Image = Properties.Resources.play;
            trackBarPosition.Value = 0;
            toolStripStatusLabelPosition.Text = getPlaybackPositionString(videoPlayback1.PlaybackPosition.Value, videoPlayback1.Length.Value);
        }

        private void trackBarPosition_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Y < 20)
            {
                trackBarPosition.Focus();
            }
            else
            {
                panelControls.Focus();
            }
        }

        private void Main_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

        private void trackBarVolume_Scroll(object sender, EventArgs e)
        {
            videoPlayback1.SetVolume(trackBarVolume.Value);
            labelVolume.Text = trackBarVolume.Value + "%";

            if (trackBarVolume.Value > 66)
            {
                buttonMute.Image = Properties.Resources.volume_max;
            }
            else if (trackBarVolume.Value > 33)
            {
                buttonMute.Image = Properties.Resources.volume_med;
            }
            else if (trackBarVolume.Value > 0)
            {
                buttonMute.Image = Properties.Resources.volume_low;
            }
            else
            {
                buttonMute.Image = Properties.Resources.volume_mute;
            }
        }

        private void trackBarPosition_MouseLeave(object sender, EventArgs e)
        {
            panelControls.Focus();
        }

        private void trackBarVolume_MouseLeave(object sender, EventArgs e)
        {
            panelControls.Focus();
        }

        private void trackBarVolume_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Y < 30)
            {
                trackBarVolume.Focus();
            }
            else
            {
                panelControls.Focus();
            }
        }

        private void buttonMinimal_Click(object sender, EventArgs e)
        {
            MinimalView();
        }

        private void MinimalView()
        {
            menuStrip.Visible = !menuStrip.Visible;
            statusStrip.Visible = !statusStrip.Visible;
            panelControls.Visible = !panelControls.Visible;
            if (videoPlayback1.Dock == DockStyle.Fill)
            {
                videoPlayback1.Dock = DockStyle.None;
                videoPlayback1.Size = new System.Drawing.Size(videoPlayback1.Resolution.Value.Width, videoPlayback1.Resolution.Value.Height);
                this.Size = new System.Drawing.Size(videoPlayback1.Resolution.Value.Width + 16, videoPlayback1.Resolution.Value.Height + 160);
                this.MinimumSize = new System.Drawing.Size(videoPlayback1.Resolution.Value.Width + 16, videoPlayback1.Resolution.Value.Height + 160);
            }
            else
            {
                videoPlayback1.Dock = DockStyle.Fill;
                this.MinimumSize = new System.Drawing.Size(videoPlayback1.Resolution.Value.Width, videoPlayback1.Resolution.Value.Height);
                this.Size = new System.Drawing.Size(videoPlayback1.Resolution.Value.Width, videoPlayback1.Resolution.Value.Height);
            }
        }

        private void Main_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Space)
            {
                videoPlayback1.TogglePause();
                if (videoPlayback1.PlaybackStatus == SimpleMediaManager.Controls.PlayState.Paused)
                {
                    buttonPause.Image = Properties.Resources.play;
                }
                else
                {
                    buttonPause.Image = Properties.Resources.pause;
                }
            }

            else if (e.KeyCode == Keys.M)
            {
                videoPlayback1.ToggleMute();
                if (videoPlayback1.Volume == 0)
                {
                    buttonMute.Image = Properties.Resources.volume_mute;
                }
                else
                {
                    buttonMute.Image = Properties.Resources.volume_max;
                }
            }

            else if (e.KeyCode == Keys.Right && e.Modifiers == Keys.Alt)
            {
                SkipPosition(10);
            }

            else if (e.KeyCode == Keys.Left && e.Modifiers == Keys.Alt)
            {
                SkipPosition(-10);
            }

            else if (e.KeyCode == Keys.Right && e.Modifiers == Keys.Control)
            {
                SkipPosition(60);
            }

            else if (e.KeyCode == Keys.Left && e.Modifiers == Keys.Control)
            {
                SkipPosition(-60);
            }

            else if (e.KeyCode == Keys.H && e.Modifiers == Keys.Control)
            {
                MinimalView();
            }
        }

        private void SkipPosition(int skip)
        {
            if (videoPlayback1.PlaybackPosition.Value + skip < 0)
            {
                trackBarPosition.Value = 0;
            }
            else if (videoPlayback1.PlaybackPosition.Value + skip > videoPlayback1.Length.Value.TotalSeconds)
            {
                trackBarPosition.Value = (int)videoPlayback1.Length.Value.TotalSeconds;
            }
            else
            {
                trackBarPosition.Value = (int)videoPlayback1.PlaybackPosition.Value + skip;
            }
        }

        private void trackBarPosition_ValueChanged(object sender, EventArgs e)
        {
            videoPlayback1.SetPlaybackPosition((double)trackBarPosition.Value);
            toolStripStatusLabelPosition.Text = getPlaybackPositionString(videoPlayback1.PlaybackPosition.Value, videoPlayback1.Length.Value);
        }
    }
}
